/**
 * Copyright 2015 jianglibo@gmail.com
 *
 */
package com.m3958.logcollector.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.m3958.logcollector.util.common.ControllerUtil;
import com.m3958.logcollector.vo.SessionKey;

/**
 * @author jianglibo@gmail.com
 *         2015年12月12日
 *
 */
@Controller
public class HomeController {

    private final ControllerUtil cutil;

    @Autowired
    public HomeController(ControllerUtil cutil) {
        this.cutil = cutil;
    }

    /**
     * 如果因为拒绝而来到这里，此时的Savedreqeust肯定有值，但是登陆之后，到达原来的目标地址，spring会自动清除这个savedRequest。
     * 保存在session里面才行。
     * 
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(value = { "/"}, method = RequestMethod.GET)
    public String loginget(HttpServletRequest request, HttpServletResponse response, Model model) {
        String cburl = cutil.getWtg(request, response);
        if (cburl != null) {
            request.getSession(true).setAttribute(SessionKey.LOGIN_CALLBACK_SESSION_KEY, cburl);
        }
        // cutil.cleanupRequestAndSession(request, response);
        // String url = cutil.getWtg(request, response);
        // url = url.substring(url.lastIndexOf("http"));
        // url = url.substring(0,url.indexOf("&"));
        // request.setAttribute("redirectURL",url);
        return "index";
    }
    
    @RequestMapping(value = {"/llapp/{noext:[^.]*}", "/llapp" }, method = RequestMethod.GET)
    public String llapp(HttpServletRequest request, HttpServletResponse response, Model model) {
        String cburl = cutil.getWtg(request, response);
        if (cburl != null) {
            request.getSession(true).setAttribute(SessionKey.LOGIN_CALLBACK_SESSION_KEY, cburl);
        }
        // cutil.cleanupRequestAndSession(request, response);
        // String url = cutil.getWtg(request, response);
        // url = url.substring(url.lastIndexOf("http"));
        // url = url.substring(0,url.indexOf("&"));
        // request.setAttribute("redirectURL",url);
        return "llapp/index";
    }
}
